﻿using System;
using System.Data;
using YouthCMS.Data;

namespace YouthCMS
{
    public partial class Students : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["Username"] == null)
            {
                Response.Redirect("Default.aspx?login=false");
            }
            CreateZipcodeList();
        }


        public void SubmitStudent(object sender, EventArgs e)
        {
            try
            {


                var studentHandler = new StudentHandler();
                var createdStudent = false;

                var tempvalue = listBoxGradeLevel.SelectedValue;
                var tempvalue2 = rBLGradeCat.SelectedValue;

                createdStudent = studentHandler.CreateStudent(txtBoxUsername.Text, txtBoxStudentPassword.Text,
                                                              txtBoxStudentSSN.Text, txtBoxStudentFirstName.Text,
                                                              txtBoxStudentLastName.Text,
                                                              txtBoxAddress1.Text, txtBoxAddress2.Text,
                                                              int.Parse(txtBoxPhone1.Text), int.Parse(txtBoxMobile.Text),
                                                              txtBoxEmail.Text,
                                                              int.Parse(ddLZipcode.SelectedValue),
                                                              int.Parse(listBoxGradeLevel.SelectedValue),
                                                              rBLGradeCat.SelectedValue.ToString(),
                                                              txtBoxSchool.Text,
                                                              bool.Parse(rBLSelectBusCard.SelectedValue));
                if (createdStudent == true)
                {
                    Response.Redirect("~/Default.aspx?created=true");
                }
            }
            catch (Exception ex)
            {

                lblAddress1.Text = ex.Data + ex.Message;
            }
        }

        public DataSet MoveList()
        {
            var dataBase = new Database();
            var adapter = dataBase.findZipcode();
            var ds = new DataSet("MyDataSet");
            adapter.Fill(ds, "Zipcode");
            return ds;
        }

        public void CreateZipcodeList()
        {
            DataSet ds = MoveList();
            DataTable dt = ds.Tables["Zipcode"];

            ddLZipcode.DataSource = dt;
            ddLZipcode.DataTextField = "Zipcode";


            ddLZipcode.DataBind();

        }





        public void GeneratePassword(object sender, EventArgs e)
        {
            const int passwordLength = 10;
            const string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
            var chars = new char[passwordLength];
            var rd = new Random();

            for (var i = 0; i < passwordLength; i++)
            {
                chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
            }


            txtBoxStudentPassword.Text = new string(chars);
        }
    }
}